package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brandController")
public class BrandController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference(check=false ,timeout=16666)
    private BrandService brandService;

    // 跳转到商品品牌展示页面
    @RequestMapping("brandList")
    public String brandList(){
        return "admin/brand";
    }

    //品牌信息查询，加分页
    @RequestMapping("getBrandInfo")
    @ResponseBody
    public Result getBrandInfo(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "8") Integer pageSize){
        try {
            logger.info("品牌查询方法开始运行{}");
            PageBean<TbBrand> brands = brandService.getBrandInfo(pageNumber, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(brands);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //品牌信息增加操作
    @RequestMapping("insertBrand")
    @ResponseBody
    public Result addBrandList(TbBrand brand){
        try {
            logger.info("品牌增加方法开始运行{}");
            brandService.insertBrand(brand);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //品牌修改回显
    @RequestMapping("updateBrandInfo")
    @ResponseBody
    public Result updateBrandInfo(Long id){
        try {
            logger.info("品牌回显方法开始运行{}");
            TbBrand brand = brandService.updateBrandInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(brand);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //品牌修改
    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updateBrand(TbBrand brand){
        try {
            logger.info("品牌修改方法开始运行{}");
            brandService.updateBrand(brand);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //品牌批量删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("品牌批量删除方法开始执行{}");
            brandService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
}
